package com.wn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wn.bean.R;
import com.wn.bean.User;
import com.wn.bean.UserRole;
import com.wn.mapper.UserMapper;
import com.wn.mapper.UserRoleMapper;
import com.wn.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.utils.PwdUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yellow Docter
 * @since 2022-05-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    /**
     *
     * 添加用户
     * 01  用户加密
     *
     * 02  用户中有rid
     *       rid存储在
     */
    @Override
    public R saveUser(User user) {

        //原始密码
        String password = user.getPassword();

        // 加密操作
        // 加密方式  盐值  散列次数
        String salt = PwdUtils.getSalt();

        String pwd = PwdUtils.getPwd(password, salt);

        // 加密的后的密码替换原始密码
        user.setSalt(salt);
        user.setPassword(pwd);

        //保存user 到db中
        int insert = userMapper.insert(user);

        if (insert > 0){
            // user_role
            UserRole userRole = new UserRole();
            userRole.setUid(user.getUid());
            userRole.setRid(user.getRid());
            userRoleMapper.insert(userRole);

        }

        return R.OK("添加成功");
    }

    /**
     *
     * 配置事务:
     * 01  配置事务管理器
     *         DataSourceTransactionManager
     * 02  开启事务
     *         @EnableTransactionManagement   // 开启事务管理
     * 03  在方法上添加事务
     *         @Transactional
     * 删除操作
     *
     * 事务失效:
     *  1. 事务注解开启没有  / 切点是否正确  (切到那个方法)
     *        @EnableTransactionManagement   // 开启事务管理
     *        @Transactional (方法没有问题  + aop )
     *        <dependency>
     *             <groupId>org.springframework.boot</groupId>
     *             <artifactId>spring-boot-starter-aop</artifactId>
     *         </dependency>
     *
     *  2.  异常的问题
     *         事务的回滚异常是: RuntimeException
     *
     *  3. 下一地方找
     *      事务和 shiro有冲突
     *      Realm 中 service 提前加载了  事务操作 的类 还未在ioc容器进行初始化
     *
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public R deleteUserById(Integer uid) {

        // 删除 user
        int i = userMapper.deleteById(uid);
        if (i <= 0 ){
            throw new RuntimeException();
        }
        //删除 user_role
        QueryWrapper<UserRole> qr = new QueryWrapper<>();
        qr.eq("uid",uid);
        int delete = userRoleMapper.delete(qr);
        if (delete <= 0 ){
            throw new RuntimeException();
        }
        return R.OK("删除成功");
    }

    @RequestMapping("")
    public R queryAll(){
        return R.OK("查询成功...");
    }

}
